<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

use think\Db;

/**
 * 赌王模型
 */
class GamblerKing extends ModelBase
{
    protected $insert = ['create_time'];

    public function getImageAttr(){
        return $this->user->avtar;
    }


    /***
     * 盈利百分比
     */
    public function getProfitPercentageAttr(){
        if($this->total_money!=0 && $this->profit!=0){
            return format_money($this->profit/$this->total_money,2);
        }else{
            return 0;
        }

    }
    /***
     * 委托数量
     */
    public function getEntrustNumberAttr(){
        return  UserKingInvestment::where(['king_id'=>$this->id,'state'=>[0,4]])->count();

    }

    /***
     * 亏损百分比
     */
    public function getLossPercentageAttr(){
        if($this->total_deficit!=0 && $this->profit!=0){
            return format_money($this->total_deficit/$this->total_money,2);
        }else{
            return 0;
        }

    }
    /**
     * 获取申请状态
     */
    public function getStateTextAttr(){
        $arr=['','待审核','通过审核','未通过审核'];
        return lang($arr[$this->state]);
    }



    /**
     * 获取总委托
     */

    public function get_total_money($where){
        $where['king_id']=$this->id;
        $where['state']=['in',[0,4]];
        return  UserKingInvestment::where($where)->sum('money')+0;
    }

    /**
     * 获取总盈利
     */

    public function get_profit_bet($where){
        $where['king_id']=$this->id;
        $where['is_winner']=1;
        $where['state']=4;
        $where['type']=4;
        return  GamesLog::where($where)->sum('profit')+0;
    }

    /**
     * 获取总排名
     */
    public function getRankingAttr(){
        $sql="SELECT m.cover_id,u.user_id,u.id,u.name,m.username,m.mobile ,SUM(d.money) AS total_money  FROM tp_gambler_king AS u INNER JOIN tp_member m on u.user_id=m.id LEFT  JOIN (SELECT *FROM  tp_user_ranking_data WHERE 1) AS d ON u.id=d.king_id  where u.state=2 GROUP BY u.id   ORDER BY total_money   DESC ";
        $king_list=Db::query($sql);
        foreach ($king_list as $ke=>$va){
            if($va['id']==$this->id){
                return $ke+1;
            }
        }
        return  lang('无排名');
    }

    /**
     * 获取总亏损
     */

    public function get_loss_bet($where){
        $where['king_id']=$this->id;
        $where['is_winner']=2;
        $where['state']=4;
        $where['type']=5;
        return  GamesLog::where($where)->sum('profit')+0;
    }


    /**
     * 获取总收益
     */

    public function get_total_bonus($where){
        $where['king_id']=$this->id;
        return  InvestmentBonusLog::where($where)->sum('bonus')+0;
    }



    /**
     * 连用户表
     */
    public function user(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }


    /**
     * 标签数组
     */
    public function getTagsArrayAttr(){
        if($this->tags){
            $tags=rtrim($this->tags, ",");
            $tags=ltrim($tags, ",");
            return explode(',',$tags);
        }else{
            return [];
        }

    }


    /**
     * 标签数组
     */
    public function getTagsNameAttr(){
        if($this->tags){
            $list=KingCategory::column('name','id');

            $tags=rtrim($this->tags, ",");
            $tags=ltrim($tags, ",");
            $tags=explode(',',$tags);
            $tag='';
            foreach ($tags as $k=>$v){
                if($k==0){
                    $tag.=$list[$v];
                }else{
                    $tag.=','.$list[$v];
                }
            }
            return $tag;
//            $lang=isset($_REQUEST['lang'])?$_REQUEST['lang']:'';
//            return   baidu_translate($tag,$lang);
        }else{
            return '';
        }

    }

    /**
     * 产品数组
     */
    public function getInvestmentArrAttr($value){
        if($value){
            return explode(',',$value);
        }else{
            return [];
        }

    }


    /**
     *赌王总委托金额
     */

    public function  getAmountEntrustedAttr(){
        $total=UserKingInvestment::where(['king_id'=>$this->id,'state'=>2])->sum('money')+0;
        return $total;
    }


    /**
     *赌王总分红金额
     */

    public function  getTotalDividendsAttr(){
        $total=UserKingInvestment::where(['king_id'=>$this->id,'state'=>2])->sum('profit')+0;
        return $total;
    }

    /**
     *赌王委托总人数
     */

    public function  getAmountNumberAttr()
    {
        $total = UserKingInvestment::where(['king_id' => $this->id, 'state' => 2])->count();
        return $total;
    }

    /***
     *历史委托人数
     */
    public function  getHistoryNumberAttr()
    {
        $total = UserKingInvestment::where(['king_id' => $this->id,'state'=>['not in',[1,3]]])->count();
        return $total;
    }

    /***
     *开通委托数
     */
    public function  getInvestmentCountAttr()
    {
        $total = GamblerKingInvestmentUser::where(['king_id' => $this->id,'state'=>['in',[1,2,3]]])->count();
        return $total;
    }

    /***
     *开通委托数
     */
    public function  getBetsCountAttr()
    {
        $total = GamesLog::where(['king_id' => $this->id,'type'=>4])->count();
        return $total;
    }

    public function getFailRateAttr(){
        $lose_total = GamesLog::where(['king_id' => $this->id,'type'=>4,'is_winner'=>2])->count();
        $total = GamesLog::where(['king_id' => $this->id,'type'=>4])->count();
        if($total){
            return  format_money($lose_total/$total*100,2);
        }
        return 0;

    }

    public function getWinningRateAttr(){
        $lose_total = GamesLog::where(['king_id' => $this->id,'type'=>4,'is_winner'=>1])->count();
        $total = GamesLog::where(['king_id' => $this->id,'type'=>4])->count();
        if($total){
            return  format_money($lose_total/$total*100,2);
        }
        return 0;
    }

//    public function  getDescribeAttr($value){
//
//        $lang=isset($_REQUEST['lang'])?$_REQUEST['lang']:'';
//        return   baidu_translate($value,$lang);
//
//
//    }
//
//    public function  getContentAttr($value){
//        $lang=isset($_REQUEST['lang'])?$_REQUEST['lang']:'';
//        return   baidu_translate($value,$lang);
//
//    }
}
